package com.bsi.login.validate.controller;

import com.bsi.core.validate.spring.group.Insert;
import com.bsi.core.validate.spring.group.Select;
import com.bsi.login.validate.entity.Product;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName ProductController
 * @Author LiuSong
 * @Date 2020/7/27 14:27
 * @Version 1.0
 * @Description 商品Spring的校验规则测试类
 */
@RestController
@RequestMapping("/")
public class ProductController {

    @GetMapping("/demo")
    public String demo(@Validated({Insert.class, Select.class}) Product product, BindingResult bindingResult) {
        if (bindingResult != null && bindingResult.getErrorCount() > 0) {
            StringBuilder builder = new StringBuilder();
            bindingResult.getAllErrors().forEach(objectError -> {
                builder.append(objectError.getDefaultMessage() + "<br/>");
            });
            return builder.toString();
        }
        return null;
    }

    @GetMapping("/demo1")
    public String demo(@Validated({Insert.class, Select.class}) Product product) {
        return "";
    }
}
